package com.xtayfjpk.security;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;

import org.junit.Test;

public class CipherTest {

	@Test
	public void testWrapMode() throws Exception {
		Cipher cipher = Cipher.getInstance("DES");
		KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
		SecretKey secretKey = keyGenerator.generateKey();
		cipher.init(Cipher.WRAP_MODE, secretKey);
		
		KeyPairGenerator generator = KeyPairGenerator.getInstance("DSA");
		PrivateKey privateKey = generator.generateKeyPair().getPrivate();
		byte[] bytes = cipher.wrap(privateKey);
		
		
		cipher.init(Cipher.UNWRAP_MODE, secretKey);
		Key unwrapKey = cipher.unwrap(bytes, "DSA", Cipher.PRIVATE_KEY);
		System.out.println(unwrapKey);
		System.out.println(unwrapKey.equals(privateKey));
	}
	
	@Test
	public void testCipherOutputStream() throws Exception {
		Cipher cipher = Cipher.getInstance("DES");
		SecretKey secretKey = readSecureKey();
		cipher.init(Cipher.ENCRYPT_MODE, secretKey);
		CipherOutputStream cipherOutputStream = new CipherOutputStream(new FileOutputStream("H:/cipher.dat"), cipher);
		DataOutputStream dataOutputStream = new DataOutputStream(cipherOutputStream);
		dataOutputStream.writeUTF("中华人民共和国");
		dataOutputStream.close();
	}
	
	@Test
	public void testCipherInputStream() throws Exception {
		Cipher cipher = Cipher.getInstance("DES");
		SecretKey secretKey = readSecureKey();
		cipher.init(Cipher.DECRYPT_MODE, secretKey);
		CipherInputStream cipherInputStream = new CipherInputStream(new FileInputStream("H:/cipher.dat"), cipher);
		DataInputStream stream = new DataInputStream(cipherInputStream);
		String result = stream.readUTF();
		System.out.println(result);
		stream.close();
	}
	
	@Test
	public void writeSecureKey() throws Exception {
		KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
		SecretKey secretKey = keyGenerator.generateKey();
		ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream("H:/sec.key"));
		stream.writeObject(secretKey);
		stream.close();
	}
	
	public SecretKey readSecureKey() throws Exception {
		ObjectInputStream stream = new ObjectInputStream(new FileInputStream("H:/sec.key"));
		SecretKey key = SecretKey.class.cast(stream.readObject());
		stream.close();
		return key;
	}
	
	@Test
	public void testSealedObject() throws Exception {
		Cipher cipher = Cipher.getInstance("DES");
		SecretKey secretKey = readSecureKey();
		cipher.init(Cipher.ENCRYPT_MODE, secretKey);
		SealedObject sealedObject = new SealedObject("中华人民共和国", cipher);
		
		Cipher cipher2 = Cipher.getInstance("DES");
		cipher2.init(Cipher.DECRYPT_MODE, secretKey);
		System.out.println(sealedObject.getObject(cipher2));
	}
}
